//
//  HKMainPageListCellModel.m
//  HKTimeKitApp
//
//  Created by huzhaoke1 on 2023/5/13.
//

#import "HKMainPageListCellModel.h"
#import "HKMainPageCell.h"
#import "HKMainPageModel.h"
#import "HKRequestDefine.h"
#import "HKWebViewController.h"
#import "HKShareWebViewControllerManager.h"

@interface HKMainPageListCellModel ()

@property (nonatomic, strong) HKWebViewController *webViewController;
@property (nonatomic, strong) HKWebViewContainer *webViewContainer;

@property (nonatomic, strong) HKMainPageModel *model;

@end

@implementation HKMainPageListCellModel

- (instancetype)initWithContent:(id)content {
    if (self = [super initWithContent:content]) {
        HKMainPageModel *model = KSSAFE_CAST(content, HKMainPageModel);
        
        self.model = model;
        HKMainPageCellViewModel *viewModel = [HKMainPageCellViewModel new];
        viewModel.imageName = model.isFromLocal ? @"secondBgImage" : @"firstBgImage";
        viewModel.title = model.type;
        viewModel.subTitle = model.name;
        viewModel.oriContent = model;
        
//        if (![[HKShareWebViewControllerManager sharedInstance] webViewContainerWithUrl:[model currentWebUrl]]) {
//            [self.webViewController loadingRequestWithUrl:[model currentWebUrl]];
//        }

        self.cellClass = [HKMainPageCell class];
        self.content = viewModel;
        self.cellHeight = [HKMainPageCell cellHeightWithContent:viewModel];
        
        [self setRender:^(__kindof HKListCellModel * _Nonnull cellModel, id  _Nonnull cell) {
            HKBaseTableViewCell *tbCell = KSSAFE_CAST(cell, HKBaseTableViewCell);
            [tbCell updateContent:cellModel.content];
        }];
        WS
        [self setCellDidSelectBlock:^(HKListCellModel * _Nonnull cellModel, NSIndexPath * _Nonnull indexPath, id  _Nonnull cell) {
            SS
            HKMainPageCellViewModel *viewModel = KSSAFE_CAST(cellModel.content, HKMainPageCellViewModel);
            [self didSelectPageModel:viewModel.oriContent];
        }];
    }
    return self;
}

- (void)reloadWebViewController {
    // [self.webViewController.webViewContainer clearCache];
    // [self.webViewController loadingRequestWithUrl:[self.model currentWebUrl]];
}

- (void)didSelectPageModel:(HKMainPageModel *)model {
    NSString *url = [model currentWebUrl];
    if (url.length == 0) return;
    HKWebViewController *webViewController = [HKWebViewController new];
    HKWebViewContainer *webViewContainer = [HKWebViewContainer new];
    [webViewContainer loadingRequestWithUrl:[model currentWebUrl]];
    webViewController.webViewContainer = webViewContainer;
    [self.context.fromViewController.navigationController pushViewController:webViewController animated:YES];
}

- (HKWebViewContainer *)webViewContainer {
    if (!_webViewContainer) {
        _webViewContainer = [HKWebViewContainer new];
    }
    return _webViewContainer;
}

- (HKWebViewController *)webViewController {
    if (!_webViewController) {
        _webViewController = [HKWebViewController new];
        _webViewController.webViewContainer = self.webViewContainer;
    }
    return _webViewController;
}

@end
